lexer grammar AnalexLexer;

options {
  backtrack=true;
  language = Java;
}


@lexer::header {
  package Practica7.ParserYacc;
}

@lexer::members {
  private int contador = 0;
  private int parentesis = 0;
  public int get_parentesis(){
    return parentesis;
  }
  public int get_contador(){
    return contador;
  }
  private ArrayList<String> errors = new ArrayList<String>();
  public void displayRecognitionError(String[] tokenNames, RecognitionException e) {
    String hdr = getErrorHeader(e);
    String msg = getErrorMessage(e, tokenNames);
    errors.add(hdr + " " + msg);
  }
   
  public ArrayList<String> getErrors() {
    return errors;
  }
}

fragment NEWLINE: '\r\n';
fragment LETTER: 'a'..'z'|'A'..'Z';
fragment DIGIT: '0'..'9';
fragment LEFTBRACKET: '[';
fragment RIGHTBRACKET: ']';

TOKEN: '%token';
START: '%start';
UNION: '%union';
LEFT: '%left';
TYPE: '%type';
PRECEDENCE: '%prec';
INCLUDE: '#include' {contador++;};
DEFINE: '#define'{contador++;};
TYPEDEF: 'typedef';
STRUCT: 'struct';
DOUBLE: 'double' {contador++;};
INT: 'int' {contador++;};
CHAR: 'char' {contador++;};
WHILE: 'while' {contador++;};
IF: 'if' {contador++;};
ELSE: 'else' {contador++;};
FOR: 'for' {contador++;};
BREAK: 'break' {contador++;};
RETURN: 'return' {contador++;};
CONTINUE: 'continue' {contador++;};
YYERROR: 'YYERROR';

WHITESPACE: (' '|'\t'|NEWLINE)  { skip(); };
PERCENT: '%';
SECTIONSEPARATOR: PERCENT PERCENT;
COMMENT: '/*' (options {greedy=false;}:.)* '*/' { skip(); };
DEFINITIONOFRULE: ':';
SEPARATOR: ';';
DOT: '.';
COMMA: ',';
UNDERSCORE: '_';
ID: (LETTER|DOT|UNDERSCORE) (LETTER|DOT|UNDERSCORE|DIGIT)*;
NUMBER: (DIGIT)+ (DOT (DIGIT)+)?|(DIGIT)+ (DOT)?;
ARRAY: ID LEFTBRACKET (ID|PSEUDOVARIABLE|NUMBER) (PLUS (ID|PSEUDOVARIABLE|NUMBER))? RIGHTBRACKET;
LITERAL: '\'' ('\\')? (.) '\'';
STRING: '"' (options {greedy=false;}:.)* '"';
OR: '|';
LOGICOPERATOR: '||'|'&&';
PLUS: '+';
MINUS: '-';
MULT: '*';
DIV: '/'; 
OPERATOR: '=';
ADDONE: PLUS PLUS;
SUBONE: MINUS MINUS;
TAG: '<' (LETTER)+ '>';
LIBRARY: '<' (LETTER)+ DOT ('h'|'H') '>';
CONDITION: '<'|'>'|'>='|'<='|'==';
LEFTPAREN: '(' {parentesis++;};
RIGHTPAREN: ')' {parentesis--;};
LEFTBRACE: '{';
RIGHTBRACE: '}';
LEFTGLOBALDEFINITION: '%{';
RIGHTGLOBALDEFINITION: '%}';
PSEUDOVARIABLE: '$' ('$'|(DIGIT)+) (DOT ID)?;